﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using Jumpton.Data;
using System.Data;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.BusinessRule.MasterData;


namespace Jumpton.SFA.Server.BusinessRule.Traffic
{
    public class TrafficManager : BusinessLogic
    {
        public DELIVERY_ROUTE Load(decimal ID)
        {
            return (DELIVERY_ROUTE)ObjectRow.Load(typeof(DELIVERY_ROUTE), string.Format("this.ID={0}", ID));
        }

        public ObjectTable GetDeliveryRoute(string domainId, string userId, string starttime, string endtime)
        {
            return new ObjectTable(typeof(DELIVERY_ROUTE_VIEW), string.Format("a.DOMAIN_ID='{0}' AND USER_ID='{1}' AND PLAN_ST between '{2}' and '{3}'", domainId, userId, starttime, endtime));
        }

        public bool Save(DELIVERY_ROUTE DeliveryRoute)
        {
            bool success = false;
            if (DeliveryRoute != null)
            {
                try
                {
                    DeliveryRoute.Save();
                    success = true;
                }
                catch (Exception err)
                {
                    message = err.Message; throw err;
                }

                
            }

            return success;
        }

        public ObjectTable GetDeliveryGPS(string domainId, string truck_code, string datetime)
        {
            datetime = datetime.Replace("-", "").Replace("/", "");
            return new ObjectTable(typeof(DELIVERY_GPS), string.Format("DOMAIN_ID='{0}' AND TRUCKS_CODE='{1}' AND  SUBSTRING(CAST(LAST_UPDATE_TIME AS VARCHAR),0,9)='{2}' order by  LAST_UPDATE_TIME ", domainId, truck_code, datetime));
        }


        public ObjectTable GetTrucks(decimal domainId, string chains_id,string acitve,string code)
        {
            string where = "";
            if (chains_id != "")
                where = " AND CHAINS_ID='" + chains_id + "' ";
            if (acitve != "")
                where += " AND ACTIVE='"+acitve+"' ";
            if (code != "")
                where += " AND CODE LIKE '%"+code+"%'";
            return new ObjectTable(typeof(TRUCKS), string.Format("DOMAIN_ID='{0}' {1} order by  LAST_UPDATE_TIME ", domainId, where));
        }

        public TRUCKS LoadTruck(decimal ID)
        {
            return (TRUCKS)ObjectRow.Load(typeof(TRUCKS), string.Format("this.ID={0}", ID));
        }
        public TRUCKS LoadTruck(string  code)
        {
            return (TRUCKS)ObjectRow.Load(typeof(TRUCKS), string.Format("this.CODE='{0}'", code));
        }

        public bool SaveTruck(TRUCKS truck)
        {
            bool success = false;
            if (truck != null)
            {
                try
                {
                    truck.Save();
                    success = true;
                }
                catch (Exception err)
                {
                    message = err.Message; throw err;
                }

               
            }

            return success;
        }

        public bool ExistsTrucks(string code)
        {
            code = code.Trim();
            code = code.Replace(" ", "");
            return (TRUCKS)ObjectRow.Load(typeof(TRUCKS), string.Format("this.CODE='{0}'", code))==null?false:true;
        }



    }
}
